<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$merchid=$this->checklogin();
if($_RQ['op']=='main'){
	$return['params']=array();
	$return['params']['pid']=0;
	$return['list']=array();
	$category=pdo_getall('jy_weishop_goods_group',array('mid'=>0,'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0,'onepluginname'=>''),array(),'id','paixu desc');
	foreach($category as &$item){
		$item['childs']=pdo_getall('jy_weishop_goods_group',array('mid'=>0,'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'id','paixu desc');
	}
	$return['category']=$category;
	$return['catlevel']=2;
	$addprice=$return['pluginset']['is_good_jinrate']?:'';
	$return['post']=array('goodsids'=>'','addprice'=>$addprice);
	set_html('title','全部商品');
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?'我的小店'.$return['pluginset']['share_title']:'全部商品-电商客',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'电商客，一个属于自己的个人商城！',
		'link'=>custom_url('jy_merch/myshop//mid='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['pluginset']['share_image']
	);
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>0,
		'status'=>1,
	);
	if(trim($_RQ['keywords'])){
		$params['title like']='%'.trim($_RQ['keywords']).'%';
	}
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
	}
	if($_RQ['pid']){
		$params['pcate']=intval($_RQ['pid']);
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
	}
	if($_RQ['order']=='isdiscount'){
		$params['isdiscount']=intval($_RQ['isdiscount']);
	}
	if($_RQ['order']=='issendfree'){
		$params['issendfree']=intval($_RQ['issendfree']);
	}
	if($_RQ['order']=='istime'){
		$params['istime']=intval($_RQ['istime']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','isbargain','title','image','price','active_plugin','active_id','salenum');
	$orderby='id desc ';
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$check=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$item['id'],'api_pluginname'=>'jy_merch'));
		if($check['id']){
			$item['isadd']=true;
		}else{
			$item['isadd']=false;
		}
		$item['buyprice']=model('jy_weishop/goods')->get_diyprice($item);
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$goodsids = explode(';',$postdata['goodsids']);	
	foreach($goodsids as $goodsid){
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		if(!$goods['id']){
			continue;
		}
		$isadd=true;
		$buyprice=model('jy_weishop/goods')->get_diyprice($goods);
		if($post['addprice']){
			if(substr($post['addprice'],-1)=='%'){
				$addprice=($buyprice*substr($post['addprice'],0,-1))/100;
			}else{
				$addprice=$post['addprice'];
			}
		}
		$data=array(
			'price'=>$buyprice+$addprice,
			'costprice'=>$buyprice,
		);
		//检查是否已添加该商品
		$check=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
		if($check){
			pdo_update('jy_weishop_goods',$data,array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
		}else{
			unset($goods['id']);
			$goods['price']=$data['price'];
			$goods['costprice']=$data['costprice'];
			$goods['merchid']=$merchid;
			$goods['pluginname']='jy_merch';
			$goods['onepluginname']='jy_merch';
			$goods['api_id']=$goodsid;
			$goods['api_pluginname']='jy_merch';
			pdo_insert('jy_weishop_goods',$goods);
			$newgoodsid=pdo_insertid();
			$specs=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			if(count($specs)){
					foreach($specs as $sk=>&$sv){
						$options=array();
						$spec_item=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sv['id'],'goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
						unset($sv['id']);
						$sv['goodsid']=$newgoodsid;
						pdo_insert('jy_weishop_goods_spec',$sv);
						$sv['newid']=pdo_insertid();
						if(count($spec_item)){
							foreach($spec_item as $ok=>$ov){
								$spec_item=$ov;
								unset($ov['id']);
								$ov['goodsid']=$newgoodsid;
								$ov['specid']=$sv['newid'];
								pdo_insert('jy_weishop_goods_spec_item',$ov);
								$spec_item['newid']=pdo_insertid();
								$options[]=$spec_item;
							}
						}
						$sv['options']=$options;
					}
			}
			$option=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			if(count($option)){
					foreach($option as $ov){
						$oldspecs=explode('_',$ov['specs']);
						$newspecs=array();
						foreach($specs as $sk=>$sp){
								foreach($sp['options'] as $op){
									if($oldspecs[$sk] == $op['id']){
										$newspecs[]=$op['newid'];
										break;
									}
								}
						}
						$ov['specs']=implode('_',$newspecs);
						$ov['goodsid']=$newgoodsid;
						unset($ov['id']);
						pdo_insert('jy_weishop_goods_option',$ov);
					}
			}
		}
	}
	if(!$isadd){
		exi('请先选择要采集的商品！','error');
	}
	exi('批量采集成功！','','/manage.selectgoods/');
}
?>